// pages/checkm/index.js
var utl = require('../../utils/util.js');
import drawQrcode from '../../utils/weapp.qrcode.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabarflag: true,
    inputText: '',
    bantk: '--',
    sk_url: '', //静态码
    bigm:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      mdate: utl.formatTime(new Date())
    })
  },
  //推送历史
  pushOrder:function(){
    wx.navigateTo({
      url: '/pages/orderlist/index',
    })
  },
  payorder:function(){
    var od = app.globalData.payord;
    if(od){
      wx.navigateTo({
        url: '/pages/orderlist/orderdetail?order=' + od,
      })
    }
   
  },
  //大额收款
  dakAction:function(e){
    this.setData({
      bigm:e.detail.value,
    })
  },

  //小额收款
  xiaoesk: function() {
    var p = this.data.xinpt;
    var nt = this.data.note;
    if (p == undefined || p == '') {
      wx.showToast({
        title: '额度不为空',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    var n = parseFloat(p);
    if (n > 1000) {
      wx.showToast({
        title: '额度过高',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/checkm/xiaoe?money=' + p + '&node=' + nt,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  //备注
  nodeAction: function(e) {
    var nde = e.detail.value;
    this.setData({
      note: nde,
    })
  },
  //查询用户卡
  queryUserCard: function() {
    utl.serverToken('/epay_apps/user/userInfoE0100', {}, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000
        })
        return;
      }
      var rsp = res.response;
      var bank = rsp.openBankName;
      var cardNm = rsp.accountNo;
      var len = cardNm.length;
      var rcode = cardNm.substring(len - 3, len);
      var bak = bank + "(尾号" + rcode + ")";
      this.setData({
        bantk: bak,
      })
    })
  },
  //去支付
  gopay: function() {
    var currnt = this.data.inputText;
    if (currnt == '') return;
    var money = parseFloat(currnt).toFixed(2);
    utl.serverToken('/epay_apps/payNew/payB0400', {
      'transAmount': money,
      'payId': '3'
    }, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000
        })
        return;
      }
      var link = res.response.payUrl;
      var i = link.indexOf("=");
      var orderid = link.substring(i + 1, link.length);
      var a = link.indexOf("?");
      var wurl = link.substring(0, a);
      wx.navigateTo({
        url: '/pages/checkm/webx?weburl=' + wurl + "&orderId=" + orderid,
      })
    })


    // wx.navigateTo({
    //   url: '/pages/payroad/index?money=' + currnt,
    // })
  },
  //小额收款金额
  skjAction: function(e) {
    var p = e.detail.value;
    this.setData({
      xinpt: p
    })

  },
  //输入金额
  jpinput: function(e) {
    console.log(e);
    var vale = e.currentTarget.dataset.info;
    var current = this.data.inputText;
    if (vale == '') return;
    if (current.length > 7) {
      wx.showToast({
        title: '输入金额超限',
        icon: 'none',
        duration: 1800
      })
      return;
    }
    if (current == '') {
      if (vale == '0' || vale == '00' || vale == '.') {
        return
      }
    }
    var dt = current.indexOf('.');
    if (vale == '.' && dt != -1) {
      return;
    }
    if (current.length - dt > 2 && dt != -1) {
      return;
    }
    current += vale;
    this.setData({
      inputText: current
    })
  },
  //删除
  reduceAction: function() {
    var cvale = this.data.inputText;
    if (cvale == '') return;
    cvale = cvale.substring(0, cvale.length - 1);
    this.setData({
      inputText: cvale,
    })
  },
  tabbarAction: function() {
    this.setData({
      tabarflag: true
    })
  },
  tabsection: function() {
    var im = this.data.sk_url;
    if (im == '') {
      // this.queryjtm();
    }
    this.setData({
      tabarflag: false
    })
  },
  //查询静态码
  queryjtm: function() {
    utl.serverToken('/epay_apps/scan/appScanH0100', {}, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000
        })
        return;
      }
      var img_url = res.response.url;
      this.draw(img_url);

    })
  },
  draw(e) {
    drawQrcode({
      width: 160,
      height: 160,
      x: 20,
      y: 20,
      canvasId: 'myQrcode',
      typeNumber: 10,
      text: e,
      image: {
        imageResource: '/pages/images/logicon.png',
        dx: 70,
        dy: 70,
        dWidth: 60,
        dHeight: 60
      },
      callback(e) {
        console.log('e: ', e)
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var mm = this.data.bigm;
    if (mm == '' || mm == undefined){
      wx.showToast({
        title: '收款金额不为空',
        icon:'none',
        duration:2000
      })
      return;
    }
    var time = this.data.mdate;
    var nike = app.globalData.userInfo.nickName;
    var uid = app.globalData.userid;
    var tel = app.globalData.telphone;
    var ordelist = { 'id': uid, 'money': mm, 'nickname': nike, 'time': time, 'node': this.data.note,'tel':tel};
    if (res.from === 'button') {}
    return {
      title: '大额收款',
      path: '/pages/index/index?oderlist=' + JSON.stringify(ordelist) ,
      success: function(res) {
        console.log('成功', res);
        
      }
    }

  },
  showImageAction: function() {
    // const irag = this.data.imgurl;
    // var arr = [irag];
    // wx.previewImage({
    //   current: '', // 当前显示图片的http链接
    //   urls: arr // 需要预览的图片http链接列表
    // })
  },
  saveAciton: function() {
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: 300,
      height: 300,
      destWidth: 300,
      destHeight: 300,
      canvasId: 'myQrcode',
      success(res) {
        console.log('图片的临时路径为：', res.tempFilePath)
        let tempFilePath = res.tempFilePath
        // 保存图片，获取地址
        // wx.saveFile({
        //   tempFilePath,
        //   success (res) {
        //     const savedFilePath = res.savedFilePath
        //     console.log('savedFilePath', savedFilePath)
        //   }
        // })

        // 保存到相册
        wx.saveImageToPhotosAlbum({
          filePath: tempFilePath,
          success: function(res) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: function(res) {
            wx.showToast({
              title: '保存失败',
              icon: 'none',
              duration: 2000
            })
          }
        })
      }
    })
  }
})